package com.blockmeta.bbs.businesslibrary.widget.chartview.Axis;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;


import com.blockmeta.bbs.baselibrary.utils.DimentionsUtil;
import com.blockmeta.bbs.businesslibrary.widget.chartview.CoinChart;
import com.blockmeta.bbs.businesslibrary.widget.chartview.CoinChart.PointLocation;

import java.util.List;

public class YAxis extends Axis
{
	private static final int TEXT_COLOR = 0xFF999999;

	protected float m_fMaxValue = 0.f;
	protected float m_fMinValue = 0.f;


	public float getMaxValue()
	{
		return m_fMaxValue;
	}

	public float getMinValue()
	{
		return m_fMinValue;
	}

	@Override
	public void drawAxis(Canvas pCanvas, Paint pPaint)
	{
		RectF tChartRect = m_callback.getChartRectF();

		PointLocation tPointlocation = m_callback.getOriginPointLocation();

		List<Point> tArrPointList = m_arrPointList;

		boolean isOnlyShowSquareNum = m_callback.isOnlyShowSquareNum();



		if (tArrPointList != null)
		{
			int tCulOffset = 1;

			int size = tArrPointList.size();

			for (int i = 0; i < size; i++)
			{
				Point tPoint = tArrPointList.get(i);
				if(m_bIsShowLine)
				{
					if(tPoint.getText().equals("0k") || tPoint.getText().equals("0"))//交易员历史画x坐标
					{
						pPaint.setColor(0xFFD3D8DE);
						pPaint.setStrokeWidth(DimentionsUtil.dp2px(1));
						pCanvas.drawLine(tPoint.getEndX(), tPoint.getEndY(), tPoint.getStartX(), tPoint.getStartY(), pPaint);
						m_fy0Value = tPoint.getEndY();
					}
				}


				if (m_bIsShowCoordinate)
				{
					pPaint.reset();
					pPaint.setColor(CoinChart.getBorderColor());

					pPaint.setStrokeWidth(1.f);
					pPaint.setAntiAlias(true);
					pPaint.setStyle(Paint.Style.STROKE);
//					PathEffect effects = new DashPathEffect(new float[]
//					{ 5, 5, 5, 5 }, 5);
//					pPaint.setPathEffect(effects);

					if ((tPoint.getStartY() - tChartRect.top) > tCulOffset && (tChartRect.bottom - tPoint.getStartY()) > tCulOffset)
					{
						Path tPath = new Path();
						tPath.moveTo(tPoint.getStartX(), tPoint.getStartY());
						tPath.lineTo(tPoint.getEndX(), tPoint.getEndY());
						pCanvas.drawPath(tPath, pPaint);
					}
				}

			}
		}
		pPaint.reset();
		pPaint.setAntiAlias(true);
	}

	@Override
	public void drawAxisText(Canvas pCanvas, Paint pPaint)
	{
		RectF tChartRect = m_callback.getChartRectF();

		PointLocation tPointlocation = m_callback.getOriginPointLocation();

		List<Point> tArrPointList = m_arrPointList;

		boolean isOnlyShowSquareNum = m_callback.isOnlyShowSquareNum();
		boolean isNeedUpDownSpace = m_callback.isNeedUpDownSpace();

		if (tArrPointList != null)
		{
			int size = tArrPointList.size();

			String[] strPercent= new String[size];
			for (int i = 0; i < strPercent.length; i++)
			{
				strPercent[i] = "";
			}
			if (size > 0)
			{
				if (m_bIsShowPercent == CoinChart.AxisPercentType.MIDDLEPERCENT)
				{
					if (size % 2 == 1)//奇数个才算
					{
						float mid = m_callback.getYesterdayClose();//   Float.parseFloat(tArrPointList.get(size / 2).getText());
						if(mid > 0.f)
						{
							for (int i = 0; i < size; i++)
							{
								float percent = Float.parseFloat(tArrPointList.get(i).getText());
								strPercent[i] = String.format("%.2f", (percent - mid) * 100 / mid) + "%";
							}
						}
					}
				}
				else if (m_bIsShowPercent == CoinChart.AxisPercentType.BOTTOMPERCENT)
				{
					float min = m_callback.getYesterdayClose();//  Float.parseFloat(tArrPointList.get(0).getText());
					if(min > 0.f)
					{
						for (int i = 0; i < size; i++)
						{
							float percent = Float.parseFloat(tArrPointList.get(i).getText());
							strPercent[i] = String.format("%.2f", (percent - min) * 100 / min) + "%";
						}
					}
				}
			}

			for (int i = 0; i < size; i++)
			{
				Point tPoint = tArrPointList.get(i);

				pPaint.reset();
				boolean shouldDrawAxis = m_bIsShowAxis && (!isOnlyShowSquareNum || (i == 0 || i == size - 1));//如果只需要显示四个角落的字
				if (shouldDrawAxis)
				{

					pPaint.setAntiAlias(true);
					pPaint.setColor(TEXT_COLOR);
					pPaint.setTextSize(m_fTextSize);

					float tYBaseLine = tPoint.getStartY() + (i == (size - 1) ? m_fTextSize : (m_fTextSize / 2.f));
					float tYTop = tYBaseLine + m_fTextSize;
					if (tYTop < tChartRect.top)
					{
						tYBaseLine = tChartRect.top + m_fTextSize + 5;
					}
					else if (tYBaseLine > tChartRect.bottom)
					{
						tYBaseLine = tChartRect.bottom - 5;
					}

					//如果是isOnlyShowSquareNum的话，就要把相应的文字缩进去一些，离边线有6px的距离
					if(isOnlyShowSquareNum)
					{
						if(i == 0)
						{
							tYBaseLine = tPoint.getStartY() - 6;
						}else if(i == size - 1)
						{
							tYBaseLine = tPoint.getStartY() + m_fTextSize + 6;
						}
					}
					else if(isNeedUpDownSpace)
					{
					    if(m_callback.isCoordinateTextOuterY()) {
							if (i == 0) {
								tYBaseLine = tPoint.getStartY() + m_fTextSize / 2;
							} else if (i == size - 1) {
								tYBaseLine = tPoint.getStartY() + m_fTextSize / 2;
							}
						}
						else {
							if (i == 0) {
								tYBaseLine = tPoint.getStartY() - 6;
							} else if (i == size - 1) {
								tYBaseLine = tPoint.getStartY() + m_fTextSize + 6;
							}
						}
					}

					if (m_callback.isCoordinateTextOuterY())
					{
						if (tPointlocation == PointLocation.TOPRIGHT)
						{
							pCanvas.drawText(tPoint.getText(), tChartRect.right + (m_callback.getYTextSpace() - pPaint.measureText(tPoint.getText())) / 2, tYBaseLine, pPaint);
						}
						else if (tPointlocation == PointLocation.BOTTOMRIGHT)
						{
							pCanvas.drawText(tPoint.getText(), tChartRect.right + (m_callback.getYTextSpace() - pPaint.measureText(tPoint.getText())) / 2, tYBaseLine, pPaint);
						}
						else if (tPointlocation == PointLocation.TOPLEFT)
						{
							pCanvas.drawText(tPoint.getText(), tChartRect.left - (m_callback.getYTextSpace() + pPaint.measureText(tPoint.getText())) / 2, tYBaseLine, pPaint);
						}
						else if (tPointlocation == PointLocation.BOTTOMLEFT)
						{
							float textWidth = pPaint.measureText(tPoint.getText());
							if(textWidth >= m_callback.getYTextSpace())
							{
								pPaint.setTextSize(DimentionsUtil.dp2px(8.f));
							}
							pCanvas.drawText(tPoint.getText(), tChartRect.left - (m_callback.getYTextSpace() + pPaint.measureText(tPoint.getText())) / 2, tYBaseLine, pPaint);
						}
					}
					else
					{
						if (tPointlocation == PointLocation.TOPRIGHT)
						{
							pCanvas.drawText(tPoint.getText(), tChartRect.right - m_callback.getYTextSpace()/6.f - pPaint.measureText(tPoint.getText()), tYBaseLine, pPaint);
						}
						else if (tPointlocation == PointLocation.BOTTOMRIGHT)
						{
							pCanvas.drawText(tPoint.getText(), tChartRect.right - m_callback.getYTextSpace()/6.f - pPaint.measureText(tPoint.getText()), tYBaseLine, pPaint);
						}
						else if (tPointlocation == PointLocation.TOPLEFT)
						{
							pCanvas.drawText(tPoint.getText(), tChartRect.left + m_callback.getYTextSpace()/6.f, tYBaseLine, pPaint);
						}
						else if (tPointlocation == PointLocation.BOTTOMLEFT)
						{
							pCanvas.drawText(tPoint.getText(), tChartRect.left + m_callback.getYTextSpace()/4.f, tYBaseLine, pPaint);
						}
					}
					if(m_bIsShowPercent != CoinChart.AxisPercentType.NOPERCENT)
					{
						if (m_callback.isCoordinateTextOuterY())
						{
							pCanvas.drawText(strPercent[i], tChartRect.right + (m_callback.getYTextSpace() - pPaint.measureText(strPercent[i])) / 2, tYBaseLine, pPaint);
						}
						else
						{
							pCanvas.drawText(strPercent[i], tChartRect.right - pPaint.measureText(strPercent[i]) - m_callback.getYTextSpace()/4.f, tYBaseLine, pPaint);
						}
					}
				}
			}
		}

		if (m_bShowTitle)
		{
			float tTextSize = DimentionsUtil.determineMaxTextSize(m_strTitle, m_callback.getYTextSpace() * 9 / 10);
			pPaint.setTextSize(tTextSize);
			pPaint.setAntiAlias(true);
			pPaint.setColor(TEXT_COLOR);

			if (tPointlocation == PointLocation.TOPRIGHT || tPointlocation == PointLocation.BOTTOMRIGHT)
			{
				pCanvas.drawText(m_strTitle, tChartRect.right + (m_callback.getYTextSpace() - pPaint.measureText(m_strTitle)) / 2, tChartRect.top + tTextSize, pPaint);
			}
			else
			{
				pCanvas.drawText(m_strTitle, tChartRect.left - (m_callback.getYTextSpace() + pPaint.measureText(m_strTitle)) / 2, tChartRect.top + tTextSize, pPaint);
			}
		}
	}

	@Override
	public void drawAxis(Canvas pCanvas, int startIndex, int xTagCount, Paint pPaint)
	{

	}
}
